Guard exploded string-struct query parameters against null ToString in C# emitter - #11439
Merged
jorgerangel-msft merged 8 commits intoJul 30, 2026
Merged
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
4 tasks
… emitter Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix null ToString() guard in C# emitter for string structs
Guard exploded string-struct query parameters against null ToString in C# emitter
Jul 29, 2026
commit: |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime crash and a serialization bug in the C# HTTP client emitter when generating exploded query parameters for string-backed extensible enums (struct enums). It ensures null/empty enum string values aren’t passed into AppendQuery(..., escape: true) and corrects dictionary explode serialization to use KeyValuePair.Value rather than the KeyValuePair itself.
Changes:
- Add
string.IsNullOrEmpty(...)guards around per-element/per-valueAppendQuerycalls for exploded arrays/dictionaries when the element type is a string-backed extensible enum. - Fix dictionary explode enum serialization to serialize
item.Value(not theKeyValuePair), correcting generated output for both string and numeric enum dictionary values. - Extend
RestClientProviderTests.TestBuildCreateRequestMethodWithQueryParametersand update the expected baseline to cover extensible string enum exploded array/dictionary cases.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/src/Providers/RestClientProvider.cs | Guards exploded query serialization for extensible string enums and fixes dictionary explode value serialization. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/RestClientProviderTests.cs | Adds new test inputs for extensible string enum exploded array/dictionary query parameters. |
| packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator.ClientModel/test/Providers/RestClientProviders/TestData/RestClientProviderTests/TestBuildCreateRequestMethodWithQueryParameters.cs | Updates expected generated output to include null/empty guards and corrected dictionary value serialization. |
…explode query params Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…y and shared helper Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…ble enum query param test Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…l guard validation Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
jorgerangel-msft
marked this pull request as ready for review
July 29, 2026 21:44
jorgerangel-msft
requested review from
JoshLove-msft,
joseharriaga,
jsquire and
m-nash
as code owners
July 29, 2026 21:44
jorgerangel-msft
approved these changes
Jul 29, 2026
jorgerangel-msft
enabled auto-merge
July 29, 2026 21:45
…nsible string enum explode Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
jorgerangel-msft
approved these changes
Jul 30, 2026
JoshLove-msft
approved these changes
Jul 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Exploded
@queryarray/dictionary parameters backed by a string struct (extensible string enum) generated code that passed.ToString()directly toAppendQuery(..., escape: true). A default-constructed value has a null internal string, so.ToString()returns null andUri.EscapeDataStringthrowsArgumentNullExceptionat runtime.Changes
RestClientProvider.BuildAppendQueryStatement): wraps eachAppendQueryinif (!string.IsNullOrEmpty(...))for array and dictionary explode when the element/value is a string-backed extensible enum. Gated by a newIsExtensibleStringEnumhelper (IsEnum && IsStruct && UnderlyingEnumType == typeof(string)) so int/float/double enums and plain values are unaffected.KeyValuePair(item) rather thanitem.Value, yielding wrong output (e.g.@param.ToSerialString(),((int)@param)). Now usesitem.Value, correcting the p6/p7 paths as well.TestBuildCreateRequestMethodWithQueryParameterswith extensible string enum array + dictionary parameters; baseline updated.Generated output now:
ToString()when serializing exploded query parameters backed by string structs #11431